/*
silverPDF is sponsored by Aleyant Systems (http://www.aleyant.com)

silverPDF is based on PdfSharp (http://www.pdfsharp.net) and iTextSharp (http://itextsharp.sourceforge.net)

Developers: Ai_boy (aka Oleksii Okhrymenko)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above information and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR SPONSORS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace ConcatenateDocuments
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            fileStream1 = Application.GetResourceStream(
                new Uri("ConcatenateDocuments;component/PDFs/HelloWorld.pdf", UriKind.RelativeOrAbsolute)).Stream;
            fileStream2 = Application.GetResourceStream(
                new Uri("ConcatenateDocuments;component/PDFs/PortableDocumentFormat.pdf", UriKind.RelativeOrAbsolute)).Stream;
            fileStream3 = Application.GetResourceStream(
                new Uri("ConcatenateDocuments;component/PDFs/SomeLayout.pdf", UriKind.RelativeOrAbsolute)).Stream;

            var fs = Application.GetResourceStream(
                new Uri("ConcatenateDocuments;component/Fonts/verdana.ttf", UriKind.RelativeOrAbsolute)).Stream;
            FontPool.Register(
                fs, new string[] { "Verdana" });
        }

        Stream fileStream1;
        Stream fileStream2;
        Stream fileStream3;

        private void saveButton1_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "PDF file format|*.pdf";

            if (d.ShowDialog() == true)
            {

                // Get some file names
                Stream[] files = new Stream[]
                {
                    fileStream1,
                    fileStream2,
                    fileStream3
                };

                // Open the output document
                PdfDocument document = new PdfDocument();

                // For checking the file size uncomment next line.
                //outputDocument.Options.CompressContentStreams = true;

                XFont font = new XFont("Verdana", 40, XFontStyle.Bold);
                XStringFormat format = XStringFormats.Center;
                int number = 0;

                // Iterate files
                foreach (var file in files)
                {
                    // Open the document to import pages from it.
                    PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

                    // Show consecutive pages facing. Requires Acrobat 5 or higher.
                    document.PageLayout = PdfPageLayout.TwoColumnLeft;

                    // Iterate pages
                    int count = inputDocument.PageCount;
                    for (int idx = 0; idx < count; idx++)
                    {
                        // Get the page from the external document...
                        PdfPage page = inputDocument.Pages[idx];
                        // ...and add it twice to the output document.
                        PdfPage page1 = document.AddPage(page);
                        PdfPage page2 = document.AddPage(page);

                        XGraphics gfx =
                          XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append);
                        DrawNumber(gfx, font, ++number);

                        gfx = XGraphics.FromPdfPage(page2, XGraphicsPdfPageOptions.Append);
                        DrawNumber(gfx, font, ++number);
                    }
                }

                // Save the document...
                document.Save(d.OpenFile());
            }
        }

        /// <summary>
        /// Draws the number in the middle of the page.
        /// </summary>
        static void DrawNumber(XGraphics gfx, XFont font, int number)
        {
            double width = 130;
            gfx.DrawEllipse(new XPen(XColors.DarkBlue, 7), XBrushes.DarkOrange,
                //new XRect((gfx.PageSize.ToXPoint() - new XSize(width, width)) / 2, 
                //          new XSize(width, width)));
              new XRect((gfx.PageSize.Width - width) / 2, (gfx.PageSize.Height - width) / 2, width, width));
            gfx.DrawString(number.ToString(), font, XBrushes.Firebrick,
              new XRect(new XPoint(), gfx.PageSize), XStringFormats.Center);
        }
    }
}
